package cs340omega;

import java.io.IOException;
import java.util.HashMap;

//This is a test Driver for the Server and Client classes
//NOT NEEDED TO RUN THE TEST DRIVER
/**
 * driver of server and client
 * testing local running states
 */
public class Communication extends Thread 
{

	private Server server;
	private final int CLIENT_PORT = 3001;
	private final int SLEEP = 500;

	/**
	 * constructor of communication class:
	 * 
	 * Run server side in local as a thread
	 * 
	 * Run client side in local as the second thread.
	 * 
	 * send 500 messages form client to server. then return the message back to client.
	 * 
	 *  
	 *  
	 */
	public Communication() throws IOException, ClassNotFoundException, InterruptedException
	{	 
		run();
		
		test(); 
		
		server.done();
	 
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		
		System.out.println("\nproject started!");
		try
		{
			@SuppressWarnings("unused")
			Communication pork = new Communication();	 
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}

	}
	/**
	 *  @see java.lang.Thread#run()
	 */
	public void run()
	{
		server = new Server();
		System.out.println("1");
		server.start();
		try 
		{
			Thread.sleep(500);
		} catch (InterruptedException e) 
		{
			e.printStackTrace();
		} 
		System.out.println("2"); 
		
		System.out.println("3");
	}
	/**
	 * sending data to local server
	 * @throws IOException
	 * @throws ClassNotFoundException
	 * @throws InterruptedException
	 */
	public void test() throws IOException, ClassNotFoundException, InterruptedException
	{
		Object[] t = new Object[2];
		t[0] = new String("A");
		Client.voidSendData("localhost", CLIENT_PORT, "void message"); 
		Client.voidSendData("localhost", CLIENT_PORT, new HashMap<Object, Object>());
		Client.voidSendData("localhost", CLIENT_PORT, new Port(3546));
		Client.sendData("localhost", CLIENT_PORT, "third message");
		Thread.sleep(SLEEP);
		System.out.println("4");
		for(int i = 0; i < 50000; i++)
		{
			if(i % 1000 == 0)
			{
				Client.voidSendData("localhost", CLIENT_PORT, "No: " + i + " void message");
				Client.sendData("localhost", CLIENT_PORT, "No: " + i + " return message");
		
			}
		} 
	}
	
	
	

}
